 #-*- coding: utf-8 -*-

from openerp import models, fields, api, exceptions

class sale_management(models.Model):
    _name = 'sale.project'
    
    name = fields.Char(string="Projectname",required=True)
    number=fields.Char(string="Projectnumber")
    address=fields.Char(string="Projectaddress",required=True)
    date_apply=fields.Datetime(string="Applydate",readonly=True,default=fields.Datetime.now())
    date_start=fields.Date(string="Startdate")
    date_end=fields.Date(string="Enddate")
    date_supply=fields.Date(string="Supplydate")
    date_signatory=fields.Date(string="Signatorydate")

    investor_id=fields.Many2one('res.partner',string="Projectinvestor")
    constructer_id=fields.Many2one('res.partner',string="Projectconstructer")
    designer_id=fields.Many2one('res.partner',string="Projectdesigner")
    builder_id=fields.Many2one('res.partner',string="Projectbuilder")
    lead_id=fields.Many2one('res.partner',string="Projectlead",required=True)
    customer_id=fields.Many2one('res.partner',string="CustomerName",index=True,required=True)
    applicant_id=fields.Many2one('res.users',string="Applicant",default=lambda self: self.env.user)
    visitlog_ids=fields.One2many('sale.visitlog','project_id',string="Visitlog")
    
    state=fields.Selection([('draft','Draf'),('confirmed','Confirmed'),('done','Done')],default='draft')
    color = fields.Integer()

    @api.one
    @api.constrains('customer_id','lead_id')
    def _check_customer_not_in_lead(self):
        if self.customer_id and self.customer_id in self.lead_id:
            raise exceptions.ValidationError("A project's customer can't be a lead")
    _sql_constraints = [
        ('number_unique',
         'UNIQUE(number)',
         "The projectnumber must be unique"),
    ]
   
    @api.one
    def action_draft(self):
        self.sudo().write({'state':'draft'})

    @api.one
    def action_confirm(self):
        self.state = 'confirmed'

    @api.one
    def action_done(self):
        self.state = 'done'
